﻿package  {
	
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.external.ExternalInterface;
	import flash.utils.Timer;
	import fl.video.MetadataEvent;  
	import fl.video.VideoEvent; 
	import fl.video.VideoProgressEvent;
	
	public class Main extends MovieClip {
		public static var main:Main;
		var Vurl:String;
		var stringArr:Array;
		var Vpoint:String;
		
		var now_time:int;//FLV当前秒数
		var get_time:String;//准备传到网页的定自义变量//当前秒数
		var total_time:int;
		var page_num:int;
		var page_get:int;
		
		var flvtime:Timer;
		
		var time_page:int;
		var time_now:int;
		
		public var bb_isMouseOn:Boolean = true;
		public var bb_Start:Boolean = false;

		public function Main() {
			main = this;
			// constructor code
			//Vurl = stage.loaderInfo.parameters["VideoUrl"];//视频路径
			Vurl = "01.flv";
			
			stringArr = [];
			//Vpoint= stage.loaderInfo.parameters["TimePoint"]; 
			Vpoint = "null";
			stringArr = Vpoint.split(",");

			page_num = stringArr.length;
			
			//以下为供js回调接口,传入参数为String类型
			ExternalInterface.addCallback("sendToActionScript1", as_GetCurrentTime);   //获取当前视频已播放时间(传入参数：1),返回值为当前时间，String类型，秒为单位
			ExternalInterface.addCallback("sendToActionScript2", as_SetPlayStop);	//控制视频停止或播放(传入参数：1为播放，0为停止),无返回
			ExternalInterface.addCallback("sendToActionScript3", as_GotoTime);		//控制视频跳到指定时间(传入参数：秒数为单位，小数点后可保留2位数字),无返回
			ExternalInterface.addCallback("sendToActionScript4", as_PlayVideo);		//过程中，控制swf播放不同的视频(传入参数：视频路径）,无返回
			//ExternalInterface.addCallback("sendToActionScript", asCallBack);
		
			this.addEventListener(Event.ADDED_TO_STAGE, addet);
			
			
			
			//测试
			//btn1.addEventListener(MouseEvent.CLICK, btn1CL);
			//btn2.addEventListener(MouseEvent.CLICK, btn2CL);
		}
		private function btn1CL(e:MouseEvent) {
			as_GetCurrentTime("1");
		}
		private function btn2CL(e:MouseEvent) {
			as_GotoTime("200.8");
		}
		function flvPlayback_metadataReceived(evt:MetadataEvent):void {  
			trace("duration:", evt.info.duration); // 16.334<br>    
			trace("framerate:", evt.info.framerate); // 15<br>    
			trace("width:", evt.info.width); // 320<br>    
			trace("height:", evt.info.height);
			bb_Start = true;
			Control.control.setTotalTime(evt.info.duration);
			if (Vpoint == "null") {
				Control.control.setTimePoint([]);
			}else{
				Control.control.setTimePoint(Vpoint.split(","));
			}
			ExternalInterface.call("jsCallBack1",String(evt.info.duration));//通知视频开始播放，并返回视频时长
		}
		function Flvpcomplete(eveObj:fl.video.VideoEvent):void {  
			trace("播完");
			bb_Start = false;
			ExternalInterface.call("jsCallBack2",String(123));//通知视频播放完，参数无意义
		}
		
		
		function as_PlayVideo(_str:String = "1.flv", TimePoint:String = "null"):void {
			Vpoint = TimePoint;
			flvPlay.stop();
			Vurl = _str;
			flvPlay.play(Vurl);

		}
		function as_SetPlayStop(_str:String = "1"):void {
			if (int(_str) == 1) {
				flvPlay.play();
			}else {
				flvPlay.pause();
			}
		}
		function as_GotoTime(_str:String = "1.5"):void {
			flvPlay.seek(Number(_str));
		}
		function as_GetCurrentTime(_str:String = "这是页数"):String {
			//如果不能有return值，就用ExternalInterface.call把参数传给js
			var time:Number = flvPlay.playheadTime;
			trace(time);
			return String(time); 
		}
		function asCallBack(_str:String="这是页数"):void
		{
			time_page = int(_str);
			if (time_page==1)
			{
				time_now = 0;
			}
			if (time_page>1)
			{
				time_now = stringArr[time_page - 2];
			}
			trace("______"+time_now)
			flvPlay.seek(time_now);
		}
		//收数据

		
		private function addet(e:Event) {
			flvPlay.source = Vurl;
			//flvtime = new Timer(1000);
			//flvtime.start();
			//flvtime.addEventListener(TimerEvent.TIMER, onTime);
			
			
			flvPlay.addEventListener(fl.video.VideoEvent.COMPLETE,Flvpcomplete);  
			flvPlay.addEventListener(MetadataEvent.METADATA_RECEIVED, flvPlayback_metadataReceived);
			this.addEventListener(Event.ENTER_FRAME, stageEF);
			flvPlay.addEventListener(VideoProgressEvent.PROGRESS, progressHandler);
			
		}
		private function stageEF(e:Event) {
			
			if (bb_Start) {
				Control.control.SetProgress(flvPlay.playheadTime);
			}
			//trace(stage.mouseX,stage.mouseY,stage.stageWidth,stage.stageHeight)
			if((stage.mouseX>670)||(stage.mouseX<0)||(stage.mouseY>350)||(stage.mouseY<0)){
				if (bb_isMouseOn) {
					bb_isMouseOn = false;
					TweenMax.killAll();
					TweenMax.to(Control.control,0.5,{alpha:0});
				}
			}else {
				if (!bb_isMouseOn) {
					bb_isMouseOn = true;
					TweenMax.killAll();
					TweenMax.to(Control.control,0.5,{alpha:1});
				}
			}
		}
		function progressHandler(event:VideoProgressEvent):void
		{
		    var bl:Number = Math.round(event.bytesLoaded/1000);
			var bt:Number = Math.round(event.bytesTotal / 1000);
			Control.control.SetLoadedProgress(bl/bt);
		}
		private function onTime(e:TimerEvent):void
		{
			now_time = flvPlay.playheadTime;
			if (now_time>=0&&now_time<stringArr[0])
			{
				page_get = 1;
				get_time = String(page_get);
			}
			for (var i:int=1; i<=page_num; i++)
			{
				if (now_time>=stringArr[i-1]&&now_time<stringArr[i])
				{
					page_get = i + 1;
					get_time = String(page_get);
				}
			}
			if (now_time>stringArr[page_num-1])
			{
				page_get = page_num + 1;
				get_time = String(page_get);
			}
			//trace(page_get);
			//trace("______"+get_time);
			ExternalInterface.call("jsCallBack",get_time);
		}
	}
	
}
